package com.jiyiren.bms;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;

import com.amap.api.maps.model.LatLng;

/**
 * Created by yiji on 2016/11/16.
 */

public class App extends Application {

    private static App mapp;
    private static SharedPreferences sp;
    @Override
    public void onCreate() {
        mapp=this;
        super.onCreate();
    }

    //同一时间只允许有一个Application实例
    public static synchronized App getAppInstance(){
        return mapp;
    }

    public synchronized SharedPreferences getSharedPreferenceInstance(){
        if(sp==null) {
            sp = getSharedPreferences(MyConfig.SHAREDPREFERENCE_NAME, Activity.MODE_PRIVATE);
        }
        return sp;
    }

    //返回用户的id
    public String getUserId(){
        return getSharedPreferenceInstance().getString(MyConfig.SHARED_USER_ID,"0");
    }

    public void setUserId(String userid){
        SharedPreferences.Editor e=getSharedPreferenceInstance().edit();
        e.putString(MyConfig.SHARED_USER_ID,userid);
        e.commit();
    }

    //获取和设置模拟用户id
    public String getSimuUserId(){
        return getSharedPreferenceInstance().getString(MyConfig.SHARED_SIMU_USER_ID,"0");
    }
    public void setSimuUserId(String simuuserid){
        SharedPreferences.Editor e=getSharedPreferenceInstance().edit();
        e.putString(MyConfig.SHARED_SIMU_USER_ID,simuuserid);
        e.commit();
    }

    //获取和设置经度纬度
    public String getLng(){
        return getSharedPreferenceInstance().getString(MyConfig.SHARED_LNG,"0");
    }
    public String getLat(){
        return getSharedPreferenceInstance().getString(MyConfig.SHARED_LAT,"0");
    }

    public void setLatLng(LatLng latLng){
        SharedPreferences.Editor e=getSharedPreferenceInstance().edit();
        e.putString(MyConfig.SHARED_LAT,latLng.latitude+"");
        e.putString(MyConfig.SHARED_LNG,latLng.longitude+"");
        e.commit();
    }

    public void setLatlng(String lng,String lat){
        SharedPreferences.Editor e=getSharedPreferenceInstance().edit();
        e.putString(MyConfig.SHARED_LNG,lng);
        e.putString(MyConfig.SHARED_LAT,lat);
        e.commit();
    }

    //设置和获得toggle按钮的状态
    public boolean getToggled(){
        return getSharedPreferenceInstance().getBoolean(MyConfig.SHARED_TOGGLE_ON,false);
    }
    public void setToggled(boolean istoggled){
        SharedPreferences.Editor e=getSharedPreferenceInstance().edit();
        e.putBoolean(MyConfig.SHARED_TOGGLE_ON,istoggled);
        e.commit();
    }
    //保存语言
    public boolean getIshasLanguage(){
        return getSharedPreferenceInstance().contains(MyConfig.SHARED_LANGUAGE);
    }
    public String getLanguage(){
        return getSharedPreferenceInstance().getString(MyConfig.SHARED_LANGUAGE,"zh");
    }

    public void setLanguage(String language){
        SharedPreferences.Editor e=getSharedPreferenceInstance().edit();
        e.putString(MyConfig.SHARED_LANGUAGE,language);
        e.commit();
    }


}
